import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TestComment {
    public void test() {
        List<Comment> allCommentList = getComments();
        int curId = 12;

        CommentThread mainThread = new CommentThread(0);
        for(Comment comment : allCommentList) {
            if(comment instanceof MainComment) {
                mainThread.addComment(comment);
            }
        }
        User testUser = new User(100,"测试员");

        System.out.println("测试评论功能");
        System.out.println("您已使用测试员的身份登录");
        System.out.println("----------------------------------------------------");
        System.out.println("进入评论区，展示所有评论");
        mainThread.displayThread();

        System.out.println("选择功能：1.按照时间顺序排序 2.按照热度进行排序 3.按照推荐进行排序");
        System.out.println("4.查看评论回复 5.为评论点赞 6.发表评论 7.回复评论");
        System.out.print("请输入要选择的功能：");
        Scanner scanner = new Scanner(System.in);

        int in = scanner.nextInt();
        while(in>=1&&in<=11){
            switch(in) {
                case 1: {
                    mainThread.setStrategy(new TimeSortStrategy());
                    System.out.println("按照时间顺序进行排序");
                    mainThread.sort();
                    mainThread.displayThread();
                    break;
                }
                case 2: {
                    System.out.println("按照热度进行排序");
                    mainThread.setStrategy(new LikeSortStrategy());
                    mainThread.sort();
                    mainThread.displayThread();
                    break;
                }
                case 3: {
                    System.out.println("按照推荐进行排序");
                    mainThread.setStrategy(new RecommendSortStrategy());
                    mainThread.sort();
                    mainThread.displayThread();
                    break;
                }
                case 4:{
                    System.out.print("请选择要查看回复的评论id:");
                    int mainId = scanner.nextInt();
                    if(mainId>=1&&mainId<=allCommentList.size()) {
                        System.out.println("展示评论"+mainId+"和它的所有回复");
                        CommentThread replyThread = new CommentThread(mainId);
                        replyThread.addComment(allCommentList.stream().filter(comment -> comment.getId()==mainId).findFirst().get());
                        for(Comment comment : allCommentList) {
                            if(comment instanceof Reply&&
                                    ((Reply) comment).getParentId()==mainId) {
                                replyThread.addComment(comment);
                            }
                        }
                        replyThread.displayThread();
                    }else {
                        System.out.println("选择的评论不存在");
                    }
                    break;
                }
                case 5:{
                    System.out.print("请选择要点赞的评论id:");
                    int mainId = scanner.nextInt();
                    if(mainId>=1&&mainId<=allCommentList.size()) {
                        System.out.println("为评论"+mainId+"点赞");
                        allCommentList.stream().filter(comment -> comment.getId()==mainId).findFirst().get().like();

                    }else{
                        System.out.println("选择的评论不存在");
                    }
                    break;
                }
                case 6:{
                    System.out.print("请输入你要发表的评论内容:");
                    String string = scanner.nextLine();
                    MainComment comment = new MainComment(curId++,testUser,string,new Timestamp(System.currentTimeMillis()));
                    allCommentList.add(comment);
                    System.out.println("您已成功发表评论");
                    mainThread.addComment(comment);
                    break;
                }
                case 7:{
                    System.out.print("请选择你要回复的评论id");
                    int mainId = scanner.nextInt();
                    if(mainId>=1&&mainId<=allCommentList.size()) {
                        System.out.print("请输入你要回复的评论内容:");
                        String string = scanner.nextLine();
                        Comment reply = new Reply(curId++,testUser,string,new Timestamp(System.currentTimeMillis()),mainId);
                        allCommentList.add(reply);
                        System.out.println("您已成功回复评论"+mainId);
                    }else{
                        System.out.println("您要回复的评论不存在");
                    }
                    break;
                }
                default:{
                    System.out.println("选择的操作不存在");
                    break;
                }
            }
            System.out.print("请输入要选择的功能：");
            in = scanner.nextInt();
        }
        System.out.println("测试完成，退出");
    }

    private static List<Comment> getComments() {
        User user1 = new User(1,"用户1");
        User user2 = new User(2,"用户2");
        User user3 = new User(3,"用户3");
        User user4 = new User(4,"用户4");
        User user5 = new User(5,"用户5");
        User user6 = new User(6,"用户6");
        User user7 = new User(7,"用户7");
        User user8 = new User(8,"用户8");
        User user9 = new User(9,"用户9");
        List<Comment> allCommentList = new ArrayList<>();
        //来点主评论
        MainComment comment1 = new MainComment(1,user1,
                "豪庭，爱听", new Timestamp(1732163725872L),20);
        Comment comment2 = new MainComment(2,user2,
                "豪庭，不爱听",
                new Timestamp(1734573725872L),15);
        Comment comment3 = new MainComment(3,user3,
                "不豪庭，爱听",
                new Timestamp(1734432515872L),18);
        Comment comment4 = new MainComment(4,user4,
                "不豪庭，不爱听",
                new Timestamp(1734483324572L),58);
        Comment comment5 = new MainComment(5,user5,
                "豪爱，不听",
                new Timestamp(1734483725872L),25);

        //回复
        Comment comment6 = new Reply(6,user9,
                "真好听吗",
                new Timestamp(1734483123872L),1);
        Comment comment7 = new Reply(7,user6,
                "豪庭喵",
                new Timestamp(1734462423872L),1);
        Comment comment8 = new Reply(8,user3,
                "不豪",
                new Timestamp(1734483731472L),1);
        Comment comment9 = new Reply(9,user2,
                "快跑",
                new Timestamp(1735323123872L),1);
        Comment comment10 = new Reply(10,user1,
                "不敢了喵，再也不听了喵",
                new Timestamp(1734483642172L),1);
        Comment comment11 = new Reply(11,user8,
                "就知道喵喵叫，再喵喵叫就变猫娘喵",
                new Timestamp(1734643573872L),1);
        comment1.setReplyCount(comment1.getReplyCount()+6);

        allCommentList.add(comment1);
        allCommentList.add(comment2);
        allCommentList.add(comment3);
        allCommentList.add(comment4);
        allCommentList.add(comment5);
        allCommentList.add(comment6);
        allCommentList.add(comment7);
        allCommentList.add(comment8);
        allCommentList.add(comment9);
        allCommentList.add(comment10);
        allCommentList.add(comment11);
        return allCommentList;
    }
}
